﻿using EShop.Net.IRepository.UnitOfWork;
using EShop.Net.IRepository.User;
using EShop.Net.IServices.User;
using EShop.Net.Loging;
using EShop.Net.Model.Entity;
using EShop.Net.Util.Encrypt;
using NLog;
using System;
using System.Threading.Tasks;

namespace EShop.Net.Services.User
{
    public class UserServices : BaseServices<UserInfo>, IUserServices
    {
        private readonly IUnitOfWork _unitOfWork;
        private readonly IUserRepository _db;
        private readonly IUserRepository userRepository;

        public UserServices(IUnitOfWork unitOfWork, IUserRepository db, IUserRepository userRepository)
        {
            this._unitOfWork = unitOfWork;
            this._db = db;
            BaseDal = db;
            this.userRepository = userRepository;
        }

        /// <summary>
        /// 商家端登录
        /// </summary>
        /// <param name="userName">账号</param>
        /// <param name="userPwd">密码</param>
        /// <returns>用户信息</returns>
        public Task<UserInfo> AdminLogin(string userName, string userPwd)
        {
            return this.userRepository.AdminLogin(userName, userPwd);
        }

        /// <summary>
        /// 查询用户根据OpenId
        /// </summary>
        /// <param name="openId">openId</param>
        /// <returns>实体数据</returns>
        public Task<UserInfo> SelectUserByOpenId(string openId)
        {
            return this.userRepository.SelectUserByOpenId(openId);
        }

        /// <summary>
        /// 更新用户sessionKey，根据openid
        /// </summary>
        /// <param name="openid">openid</param>
        /// <param name="sessionKey">sessionKey</param>
        /// <param name="nickName">昵称</param>
        /// <param name="avatarImage">头像路径</param>
        /// <returns>成功或失败</returns>
        public Task<bool> UpdateByUserOpenid(string openid, string sessionKey, string nickName, string avatarImage)
        {
            return this.userRepository.UpdateByUserOpenid(openid, sessionKey, nickName, avatarImage);
        }

        /// <summary>
        /// 根据ID修改个人信息
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="birthday">生日</param>
        /// <param name="gender">性别</param>
        /// <param name="city">城市</param>
        /// <param name="province">省份</param>
        /// <param name="district">区</param>
        /// <param name="userPhone">手机号</param>
        /// <returns>成功或失败</returns>
        public async Task<bool> UpdateUserInfoById(int id, DateTime birthday, int gender, string city, string province, string district, string userPhone)
        {
            try
            {
                string key = EncryptionHelper.GenerateMD5Key();
                string userPhoneEncrypt = key + "。" + EncryptionHelper.MD5Encrypt(userPhone, key);  // 经过加密的手机号
                return await this.userRepository.UpdateUserInfoById(id, birthday, gender, city, province, district,  userPhoneEncrypt);
            }
            catch (Exception ex)
            {
                NLogUtil.WriteAll(LogLevel.Error, LogType.BusinessLogic, "根据ID修改个人信息", "UpdateUserInfoById业务逻辑层", ex);
                return false;
            }
        }
    }
}
